using Engine;
using Game;
using GameEntitySystem;

namespace SCIENEW {
    /// <summary>
    /// 机械动力引擎的子系统
    /// </summary>
    public class SubsystemEnginePower : Subsystem {
        public Dictionary<Point3, IEngineComponent> m_engines = new();

        public void Add(Point3 point, IEngineComponent engine) {
            if (!m_engines.TryAdd(point, engine)) {
                m_engines[point] = engine;
            }
        }

        public void Remove(Point3 point) {
            m_engines.Remove(point);
        }

        public bool IsPowered(Point3 position, out IEngineComponent engineComponent) {
            engineComponent = null;
            for (int face = 0; face < 6; face++) {
                Point3 testPoint = position + CellFace.FaceToPoint3(face);
                if (m_engines.TryGetValue(testPoint, out IEngineComponent engine) && engine.IsPowering()) {
                    engineComponent = engine;
                    return true;
                }
            }
            return false;
        }
    }
}